﻿@using SCMS.Resources
@model IEnumerable<SCMS.UI.Models.ViewPOs>
@{
    ViewBag.Title = Resources_frank.Global_String_PurchaseOrders;
}
<script src="@Url.Content("~/Scripts/js/modalite.js")" type="text/javascript"></script>
<div class="partialViewCard">
    <div class="partialViewCardHeader">
        <h3>
            @Resources_frank.Global_String_PurchaseOrders</h3>
    </div>
    <br />
    @if (Model.Count() > 0)
    {
        <div id="tablewrapper">
            <div id="tableheader">
                <div class="search">
                    <b>@Resources_frank.Global_String_SearchBy:</b><br />
                    <select id="columns" onchange="sorter.search('query')">
                    </select>
                    <input type="text" id="query" onkeyup="sorter.search('query')" />
                </div>
                <span class="details">
                    <div>
                        @Resources_frank.Global_String_Records <span id="startrecord"></span>-<span id="endrecord"></span> @Resources_frank.Global_String_Of <span id="totalrecords">
                        </span>
                    </div>
                </span>
            </div>
            <br /><br />
            <div id="export-panel">
                <a href="#" id="to-pdf" title="Export Selection to Pdf">Export Selection to Pdf</a>
                <a href="#" id="to-msexcel" title="Export Selection to Excel">Export Selection to Excel</a>
                <div id="export-info">Export</div>
            </div>
            <table width="98%" align="center" cellpadding="0" cellspacing="0" border="0" id="table" class="tinytable">
                <thead>
                    <tr>
                        <th><h3>@Resources_frank.CompletionCerticate_ViewCC_PONo</h3></th><th><h3>@Resources_frank.Global_String_ORNo</h3></th>
                        <th><h3>@Resources_frank.Global_String_Supplier</h3></th><th><h3>@Resources_frank.Global_String_DelivDate</h3></th>
                        <th><h3>@Resources_frank.Global_String_DelivAddress</h3></th><th><h3>@Resources_frank.Global_String_POValue</h3></th>
                        <th><h3>@Resources_frank.Global_String_Status</h3></th><th><h3>@Resources_frank.Global_String_StatusDate</h3></th>                        
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr data-purchase-id="@item.POEntiy.Id" onclick="sendRequest('/PurchaseOrder/ViewPurchaseOrdersDetails/@item.POId', 'PODiv', 'loading')" style=" cursor:pointer;">
                            <td align="center">
                                @item.POEntiy.RefNumber
                            </td>
                            <td align="center">
                                @if (item.POEntiy.OrderRequest != null)
                                {
                                    @item.POEntiy.OrderRequest.RefNumber
                                }
                            </td>
                            <td>
                                @item.supplier
                            </td>
                            <td align="right">
                                @item.POEntiy.LatestDeliveryDate.ToString("dd/MM/yyyy")
                            </td>
                            <td>
                                @item.POEntiy.Location.Name
                            </td>
                            <td align="right">
                                @item.POEntiy.TotalAmount.Value.ToString("#,##0.00")
                            </td>
                            <td align="center">
                                @if (item.POEntiy.IsAuthorized == true)
                                { 
                                    <b style="color: Green; font-weight: bold;">
                                        @Resources_frank.Global_String_StatusAU
                                    </b>
                                }
                                else if (item.POEntiy.IsRejected)
                                {
                                    <b style="color: Red; font-weight: bold;">
                                        @Resources_frank.Global_String_StatusRJ
                                    </b>
                                }
                                else if (item.POEntiy.IsReviewed == true)
                                {
                                    @Resources_frank.Global_String_StatusRV
                                }
                                else if (item.POEntiy.IsSubmitted == true)
                                { 
                                    @Resources_frank.Global_String_StatusCR
                                }
                                else
                                { 
                                    @Resources_frank.Global_String_StatusNEW
                                }
                            </td>
                            <td align="right">
                                @if (item.POEntiy.IsAuthorized == true)
                                { 
                                    @item.POEntiy.AuthorizedOn.Value.ToString("dd/MM/yyyy")
                                }
                                else if (item.POEntiy.IsRejected == true)
                                {
                                    if (item.POEntiy.AuthorizedOn.HasValue)
                                    { 
                                        @item.POEntiy.AuthorizedOn.Value.ToString("dd/MM/yyyy")
                                    }
                                    else
                                    { 
                                        @item.POEntiy.ReviewedOn.Value.ToString("dd/MM/yyyy")
                                    }
                                }
                                else if (item.POEntiy.IsReviewed == true)
                                {
                                    @item.POEntiy.ReviewedOn.Value.ToString("dd/MM/yyyy")
                                }
                                else if (item.POEntiy.IsSubmitted == true)
                                { 
                                    @item.POEntiy.PreparedOn.Value.ToString("dd/MM/yyyy")
                                }
                                else
                                { 
                                    @item.POEntiy.PreparedOn.Value.ToString("dd/MM/yyyy")
                                }
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
            <div id="tablefooter">
                <div id="tablenav">
                    <div>
                        <img src="../../Content/images/pager/first.gif" width="16" height="16" alt="@Resources_frank.Global_String_FirstPage"
                            onclick="sorter.move(-1,true)" />
                        <img src="../../Content/images/pager/previous.gif" width="16" height="16" alt="@Resources_frank.Global_String_PreviousPage"
                            onclick="sorter.move(-1)" />
                        <img src="../../Content/images/pager/next.gif" width="16" height="16" alt="@Resources_frank.Global_String_NextPage"
                            onclick="sorter.move(1)" />
                        <img src="../../Content/images/pager/last.gif" width="16" height="16" alt="@Resources_frank.Global_String_LastPage"
                            onclick="sorter.move(1,true)" />
                    </div>
                    <div>
                        <select id="pagedropdown">
                        </select>
                    </div>
                    <div>
                        <a href="javascript:sorter.showall()">@Resources_frank.Global_String_ViewAll</a>
                    </div>
                </div>
                <div id="tablelocation">
                    <div>
                        <select onchange="sorter.size(this.value)">
                            <option value="5">5</option>
                            <option value="10">10</option>
                            <option value="20" selected="selected">20</option>
                            <option value="50">50</option>
                            <option value="100">100</option>
                        </select>
                        <span>@Resources_frank.Global_String_EntriesPerPage</span>
                    </div>
                    <div class="page">
                        @Resources_frank.Global_String_Page <span id="currentpage"></span>@Resources_frank.Global_String_Of <span id="totalpages"></span>
                    </div>
                </div>
            </div>
        </div>
    }
    else
    {
        <i>@Resources_frank.PurchaseOrder_ViewPurchaseOrders_NoPurchaseOrders</i>
    }
    <br />
</div>
<script type="text/javascript">
    jQuery(document).ready(function () {

        jQuery("#to-pdf").click(function (e) {

            e.preventDefault();
            if (jQuery("#to-pdf").data('in-progress') == 'TRUE') {
                return;
            }
            jQuery('#export-panel').toggleClass('ajax-loading-indicator');

            jQuery("#to-pdf").data('in-progress', 'TRUE');

            var matches = jQuery("tr[data-included='YES']");
            var ids = '';
            //console.log(matches.length);

            jQuery.each(matches, function (index, value) {
                ids += jQuery(value).data('purchase-id') + '|';
            });

            //console.log(ids);
            jQuery.post("/Reports/PO/SelectedPurchaseOrders", { ids: ids },
                function (data) {

                    if (data == '#N/A') {
                        $.modalite('<p class="error">No Results</p>', {
                            theme: 'dark',
                            width: 300
                        });

                    } else {

                        $.modalite('<iframe style="border: none; width: 900px; height: 500px;" src="' + data + '"></iframe>', {
                            width: 900,
                            theme: 'dark'
                        })
                    }
                    jQuery("#to-pdf").data('in-progress', '');
                    jQuery('#export-panel').toggleClass('ajax-loading-indicator');
                });
        });

        jQuery("#to-msexcel").click(function (e) {
            return;
            e.preventDefault();
            if (jQuery("#to-msexcel").data('in-progress') == 'TRUE') {
                return;
            }
            jQuery('#export-panel').toggleClass('ajax-loading-indicator');

            jQuery("#to-msexcel").data('in-progress', 'TRUE');

            var matches = jQuery("tr[data-included='YES']");
            var ids = '';

            jQuery.each(matches, function (index, value) {
                ids += jQuery(value).data('order-id') + '|';
            });

            //console.log(ids);
            jQuery.post("/Reports/OrderRequest/SelectedOrderRequestsExcel", { ids: ids },
                function (data) {
                    $('#ajax-loader').hide();
                    if (data == '#N/A') {

                        $.modalite('<p class="error">No Results</p>', {
                            theme: 'dark',
                            width: 300
                        });

                    } else {

                        dynamicIframe({ url: data });
                    }
                    jQuery("#to-msexcel").data('in-progress', '');
                    jQuery('#export-panel').toggleClass('ajax-loading-indicator');
                });
        });

    });

</script>

